package com.woniuxy.amusementparkos.service.fzw.impl;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.amusementparkos.dao.fzw.UserDao;
import com.woniuxy.amusementparkos.entity.fzw.User;
import com.woniuxy.amusementparkos.exception.fzw.UserException;
import com.woniuxy.amusementparkos.service.fzw.UserService;

@Service
public class UserServiceImp implements UserService{
	@Autowired
	UserDao userDao;
	@Override
	public void register(User user) {
		//检验用户账号是否已存在
		User realuser = userDao.selectOneUserByName(user.getUsername());
		if(realuser==null) {
			throw new UserException("账号已存在");
		}
		//若账号不存在，则允许注册
		//密码加密,返回盐值和加密密码
		String[] encryptPassword = UserService.encryptPassword(user.getUsername(),user.getPassword());
		user.setSalt(encryptPassword[0]);
		user.setPassword(encryptPassword[1]);
		
		userDao.register(user);
	
	}
	@Override
	public User selectOneUserByName(String username) {
		User user = userDao.selectOneUserByName(username);
		return user;
	}
	@Override
	public String selectPasswordByUsername(String username) {
		String realPassword=userDao.selectPasswordByUsername(username);
		return realPassword;
	}
	/**
	 * 查询登录用户的所有角色信息
	 */
	@Override
	public Collection<String> selectAllRoleByUsername(String principal) {
		Collection<String> roles=userDao.selectAllRoleByUsername(principal);
		return roles;
	}
	@Override
	public Collection<String> selectAllPermissionByRoles(Collection<String> roles) {
		Collection<String> permissions=userDao.selectAllPermissionByRoles(roles);
		return permissions;
	}
	/**
	 * 根据用户名查询对应的真实姓名
	 */
	@Override
	public String selectAccountByUsername(String username) {
		return userDao.selectAccountByUsername(username);
	}
	/**
	 * 根据用户名查用户user
	 */
	@Override
	public User selectUserByUsername(String username) {
		return userDao.selectUserByUsername(username);
	}
	/**
	 * 根据用户id查询所有的菜单
	 */
	@Override
	public List<String> selectMenusByUserId(int user_id) {
		return userDao.selectMenusByUserId(user_id);
	}
	
}
